﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Foothill.Common.Logging;
using Foothill.DataAccess.Interface;
using Foothill.Model;

namespace Foothill.DataAccess.Repository
{
    public class MiscInfoRepo : DbAccessBase, IMiscInfoRepo
    {
        public IDictionary<int, string> GetCities(int provinceId)
        {
            IDictionary<int, string> result = new Dictionary<int, string>();

            string sql = "SELECT [CityId],[CityName] FROM [City] WHERE provinceId=@provinceId";

            DbCommand dbCommand = FoothillDB.GetSqlStringCommand(sql);
            FoothillDB.AddInParameter(dbCommand, "provinceId", DbType.Int32, provinceId);

            try
            {
                using (IDataReader reader = FoothillDB.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        result.Add(reader.GetInt32(0), reader.GetString(1));
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error(FoothillModule.DataAccess, exception.Message, exception);
                throw new DataAccessException(string.Format("Error when getting cities for province id ={0}", provinceId), exception);
            }

            return result;
        }

        public IDictionary<int, string> GetOrderByOptions(string cultureCode)
        {
            IDictionary<int, string> result = new Dictionary<int, string>();

            string sql = "SELECT [OrderByOptionCode],[OrderByOptionName] FROM [OrderByOption] WHERE [CultureCode]=@cultureCode";
            DbCommand dbCommand = FoothillDB.GetSqlStringCommand(sql);

            FoothillDB.AddInParameter(dbCommand, "cultureCode", DbType.String, cultureCode);

            try
            {
                using (IDataReader reader = FoothillDB.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        result.Add(reader.GetInt32(0), reader.GetString(1));
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error(FoothillModule.DataAccess, exception.Message, exception);
                throw new DataAccessException(string.Format("Error when getting order by options for cultureCode ={0}", cultureCode), exception);
            }

            return result;
        }
    }
}
